//
//  YGStatus.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/15.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGStatus: NSObject {
    
    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id:	Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String? {
        didSet {
            if source == nil || source?.characters.count == 0 {
//                YGPrint(items: "返回的来源有问题")
                return
            }
            ///  "<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>"
            // 截取需要的内容
            let nsSource = source! as NSString
//            YGPrint(items: "\(source)")
            let firstRange = nsSource.rangeOfString(">")
//            YGPrint(items: "\(firstRnage)")
            let lastRange = nsSource.rangeOfString("</")
            let range = NSRange(location: firstRange.location + 1, length: lastRange.location - firstRange.location - 1)
            let newSource = nsSource.substringWithRange(range)
//            YGPrint(items: "\(newSource)")
            source = "来自 " + newSource
        }
    }
    
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数
    var attitudes_count: Int = 0
    
    /// 配图数组，数组里是字典
    var pic_urls: [[String: AnyObject]]? {
        didSet {
            guard let urlDics = pic_urls else {
                YGPrint(items: "配图没有值")
                return
            }
            // 定义空数组
            pictureURLs = [NSURL]()
            for dic in urlDics {
                if let urlString = dic["thumbnail_pic"] as? String {
                    let url = NSURL(string: urlString)
                    pictureURLs?.append(url!)
                }
            }
        }
    }
    
    /// 模型提供URL数组
    var pictureURLs: [NSURL]?
    
    /// 用户模型
    var user: YGUser?
    
    /// 被转发微博模型
    var retweeted_status: YGStatus?
    
    init(dic: [String: AnyObject]) {
        super.init()
    
        setValuesForKeysWithDictionary(dic)
    }
    
    /// 缓存的行高
    var rowHeight: CGFloat?
    
    // 拦截"user"的key,自己转换模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dic = value as? [String: AnyObject] {
                user = YGUser(dic: dic)
            }
            return
        }
        
        if key == "retweeted_status" {
            if let dic = value as? [String: AnyObject] {
                retweeted_status = YGStatus(dic: dic)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    // 找不到对应属性会调用
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    /// 对象打印
    override var description: String {
        get {
            let keys = ["created_at", "id", "text", "source", "reposts_count", "comments_count", "attitudes_count", "pic_urls"]
            
            return "\n \t 微博模型\(dictionaryWithValuesForKeys(keys))"
        }
    }

}


























